package com.jerry.consumer.controller;

import com.jerry.common.bean.Product;
import com.jerry.common.bean.User;
import com.jerry.consumer.client.ProductClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shop/product")
public class ProductController {

	@Value("${provider.productUrl}")
	private String productUrl;

	@Autowired
	private ProductClient productClient;
	@Autowired
	private DiscoveryClient discoveryClient;

	//查询产品列表
	@SuppressWarnings("unchecked")
	@GetMapping("/list")
	@HystrixCommand(fallbackMethod="hystrix_list")
	public List<Product> getProductList() {
		List<Product> productList = productClient.list();
		return productList;
	}

	//根据id查询产品
	@GetMapping("/get/{id}")
	@HystrixCommand(fallbackMethod="hystrix_get")
	public Product getProduct(@PathVariable("id") Long id) {
		return productClient.get(id);
	}

	//增加一个产品
	@GetMapping("/add")
	@HystrixCommand(fallbackMethod="hystrix_add")
	public boolean addProduct(Product product) {
		return productClient.add(product);
	}

	//增加一个产品
	@GetMapping("/update")
	@HystrixCommand(fallbackMethod="hystrix_update")
	public boolean updateProduct(Product product) {
		return productClient.update(product);
	}

	//删除一个产品
	@GetMapping("/delete/{id}")
	@HystrixCommand(fallbackMethod="hystrix_delete")
	public void deleteProduct(@PathVariable("id") Long id) {
		productClient.delete(id);
	}

	//查询provider-product的实例信息
	@GetMapping("/productInstance")
	public List<ServiceInstance> productInstance() {
		return this.discoveryClient.getInstances("provider-product");
	}



	/////////////////////////// 熔断处理 开始////////////////////////////////////
	public List<Product> hystrix_list() {
		List<Product> productList = new ArrayList<Product>();
		Product product = new Product();
		product.setId(-1l);
		product.setName("数据库存中没有数据--@HystrixCommand");
		product.setPrice(0.0f);
		product.setQuantity(0);
		productList.add(product);
		return  productList;
	}

	public Product hystrix_get(@PathVariable("id") Long id ) {
		Product product = new Product();
		product.setId(-1l);
		product.setName("数据库存中没有数据--@HystrixCommand");
		product.setPrice(0.0f);
		product.setQuantity(0);
		return  product;
	}

	public boolean hystrix_add(Product product) {
		System.out.println("新增方法发生异常");
		return false;
	}

	public boolean hystrix_update(Product product) {
		System.out.println("更新方法发生异常");
		return false;
	}

	public void hystrix_delete(Long id) {
		System.out.println("删除方法发生异常");
	}
	/////////////////////////// 熔断处理 结束////////////////////////////////////

}
